package com.et.drive

import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.machine.convert.Inlets
import com.et.protocol.core.ETInlets
import com.et.protocol.core.IGuide
import com.et.protocol.core.IObserver
import java.io.File
import java.util.concurrent.Executors

object TaskCenter : Inlets {

    private val thread = Executors.newSingleThreadExecutor()
    private val guides = HashMap<String, IGuide>()

    private fun start(p: String = "ttymxc1") {
        thread.execute {
            if (guides.containsKey(p)) {
                ELog.d(TAGS.machine, "$p exist")
            } else {
                ELog.d(TAGS.machine, "$p create new instance")
                val file = File("/dev/$p")
            }
        }
    }

    fun getGuide(port: String): IGuide? {
        return guides[port]
    }

    override fun init() {
        start()
    }

    override fun exit() {
        // TODO
    }

}


object Observer : IObserver {

    private val observers = HashMap<String, IObserver>()

    fun subscribe(className: String, observer: IObserver) {
        observers[className] = observer
    }

    override fun action1(code: Int) {
        if (observers.isNotEmpty()) {
            observers.forEach {
                it.value.action1(code)
            }
        }
    }

    override fun action2(code: Int, data: ByteArray) {

    }

    override fun action3(code: Int, data: ByteArray) {

    }
}

object TaskSignIn {
    fun go(byteArray: ByteArray) {
        ELog.d(TAGS.machine, "${byteArray.toString()}")
    }
}